Skip to content

Method: createId(Class, Object)

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.util;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.concurrent.atomic.AtomicInteger;
30:
31: /***************************************************************************************************************************************************************
32: *
33: * A factory for creating a new, unique {@code Id} for an object.
34: *
35: * @author Fabrizio Giudici
36: * @it.tidalwave.javadoc.stable
37: *
38: **************************************************************************************************************************************************************/
39: @FunctionalInterface
40: public interface IdFactory
41: {
42: /** Shortcut for {@link it.tidalwave.util.As}. */
43: public static final Class<IdFactory> _IdFactory_ = IdFactory.class;
44:
45: /** A default implementation that uses UUID.
46: * @since 3.2-ALPHA-19 */
47: public static final IdFactory DEFAULT = Id::ofUuid;
48:
49: /***********************************************************************************************************************************************************
50: * Creates a new mock factory, useful for testing, that returns mock UUIDs based on a sequential counter.
51: *
52: * @return the new factory
53: * @since 3.2-ALPHA-23
54: **********************************************************************************************************************************************************/
55: @Nonnull
56: public static IdFactory createMock()
57: {
58: final var sequence = new AtomicInteger(0);
59: return () -> Id.of(String.format("%08x-0000-0000-0000-000000000000", sequence.getAndIncrement()));
60: }
61:
62: /***********************************************************************************************************************************************************
63: * Creates a new id.
64: *
65: * @return the new id
66: **********************************************************************************************************************************************************/
67: @Nonnull
68: public Id createId();
69:
70: /***********************************************************************************************************************************************************
71: * Creates a new id for an object of the given class.
72: *
73: * @param objectClass the class of the object for which the {@code Id} is created
74: * @return the new id
75: **********************************************************************************************************************************************************/
76: @Nonnull
77: public default Id createId (@Nonnull Class<?> objectClass)
78: {
79: return createId();
80: }
81:
82: /***********************************************************************************************************************************************************
83: * Creates a new id for the given object of the given class. This method allows to explicitly pass a {@code Class}
84: * for cases in which the {@code object} implements multiple interfaces and one wants to specify the referenced one.
85: *
86: * @param object the object for which the {@code Id}
87: * @param objectClass the class of the object for which the {@code Id} is created
88: * @return the new id
89: **********************************************************************************************************************************************************/
90: @Nonnull
91: public default Id createId (@Nonnull Class<?> objectClass, @Nonnull Object object)
92: {
93: return createId(objectClass);
94: }
95: }